package com.freesky.biz.rights.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsBusiEntity;
import com.freesky.biz.rights.service.IRightsActivityService;
import com.freesky.biz.rights.service.IRightsBusiService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.QueryWrapperUtils;
import com.freesky.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 权益业务管理
 */
@RestController
@RequestMapping("/rights/busi")
public class RightsBusiController extends BaseController {

    @Autowired
    private IRightsBusiService rightsBusiService;

    /**
     * 分页获取权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:list')")
    @GetMapping("/page")
    public TableDataInfo page(RightsBusiEntity rightsBusi) {
        QueryWrapper<RightsBusiEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsBusi);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByDesc("create_time");
        IPage<RightsBusiEntity> pageList = rightsBusiService.page(plusPage(),queryWrapper);
        return getDataTable(pageList);
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(rightsBusiService.getById(id));
    }

    /**
     * 同步缓存
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:sync')")
    @GetMapping(value = "/cacheSync/{id}")
    public AjaxResult cacheSync(@PathVariable String id) {
        return rightsBusiService.cacheSync(id);
    }

    /**
     * 重置
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:sync')")
    @GetMapping(value = "/resetSync/{id}")
    public AjaxResult resetSync(@PathVariable String id) {
        return rightsBusiService.resetSync(id);
    }

    @PreAuthorize("@ss.hasPermi('rights:busi:audit')")
    @PostMapping(value = "/audit")
    public AjaxResult audit(@RequestBody RightsBusiEntity rightsBusi) {
        return toAjax(rightsBusiService.auditBusi(rightsBusi.getId(),
                rightsBusi.getAuditFlag(),rightsBusi.getAuditReason()));
    }

    /**
     * 新增权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:add')")
    @Log(title = "权益业务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RightsBusiEntity rightsBusi) {
        if(!rightsBusiService.checkNameUnique(rightsBusi)){
            return AjaxResult.success("新增业务 "+rightsBusi.getBusiName()+"已存在，请重新分配!");
        }
        rightsBusi.setId(rightsBusiService.generalBusiId());
        rightsBusi.setDelFlag("0");
        rightsBusi.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsBusi.setCreateTime(new Date());
        rightsBusi.setUpdateTime(new Date());
        return toAjax(rightsBusiService.save(rightsBusi));
    }

    /**
     * 修改权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:edit')")
    @Log(title = "权益业务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RightsBusiEntity rightsBusi) {
        RightsBusiEntity queryData = rightsBusiService.getById(rightsBusi.getId());
        if(queryData == null){
            return AjaxResult.success("权益业务信息不存在，无法修改!");
        }
        if(!rightsBusiService.checkNameUnique(rightsBusi)){
            return AjaxResult.success("修改权益业务 "+rightsBusi.getBusiName()+"已存在，请重新分配!");
        }
        rightsBusi.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsBusi.setUpdateTime(new Date());
        return toAjax(rightsBusiService.updateById(rightsBusi));
    }

    /**
     * 删除权益业务
     */
    @PreAuthorize("@ss.hasPermi('rights:busi:remove')")
    @Log(title = "权益业务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(rightsBusiService.deleteRightsBusByIds(ids.toArray(new String[0])));
    }
}
